package com.fckbug.action.user;

import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.fckbug.service.UserService;
import com.fckbug.util.SpringContextHolder;
import com.mysql.jdbc.StringUtils;

public class RegCheckAction extends Action {
	UserService userService = (UserService)SpringContextHolder.getBean("userService");
	
	public ActionForward execute(ActionMapping mapping, ActionForm form,
	            HttpServletRequest request, HttpServletResponse response) throws Exception {
		String what = request.getParameter("what");//校验哪项
		String value = request.getParameter("val");//项值
		
		String result="";
		
		if(StringUtils.isNullOrEmpty(what) || StringUtils.isNullOrEmpty(value)) {
			result="[-1]";
		} else {
			try{
				if("username".equals(what)) {
					List list = userService.getUserByUsername(value);
					if(list.size()>0) {
						result = "[1]";//已存在
					} else {
						result = "[0]";//不存在，可以使用
					}
				} else if("email".equals(what)) {
//					if(searchDao.isExist("userinfo", "email", value)) {
//						result = "[1]";//已存在
//					} else {
						result = "[0]";//不存在，可以使用
//					}
				}
//				response.setHeader("Content-Type", "text/html;charset=UTF-8");
				
			} catch(Exception ex) {
				ex.printStackTrace();
			} finally {
			}
		}
		
		PrintWriter pw = response.getWriter();
		pw.print(result);
		pw.close();
		
		return null;
	}
}
